
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Doctorpanel extends javax.swing.JFrame {

    /**
     * Creates new form Doctorpanel
     */
    String CRD, CRDIDT, CRDPT, CRQT, CRPrescription, CRPN;
    JFileChooser fc;
    File file = null;

    DefaultTableModel consultantReportdtm, searchReportdtm;

    ButtonGroup buttonGroup;

    public Doctorpanel() {
        initComponents();
        currentDate();
        consultantReportdtm = new DefaultTableModel();
        consultationPatientHistory.setModel(consultantReportdtm);
        consultantReportdtm.addColumn("Date");
        consultantReportdtm.addColumn("Patient Name");
        consultantReportdtm.addColumn("Illness Description");
        consultantReportdtm.addColumn("Prescription");
        consultantReportdtm.addColumn("Quantity");
        consultantReportdtm.addColumn("Type");

        searchReportdtm = new DefaultTableModel();
        searchReportHistory.setModel(searchReportdtm);
        searchReportdtm.addColumn("Date");
        searchReportdtm.addColumn("Patient Name");
        searchReportdtm.addColumn("Illness Description");
        searchReportdtm.addColumn("Prescription");
        searchReportdtm.addColumn("Quantity");

        buttonGroup = new ButtonGroup();
        buttonGroup.add(consultationPrescripPillsBtn);
        buttonGroup.add(consultationPrescripLiquidBtn);

        setTitle("Doctor UI");
    }

    public void currentDate() {
        Date currentDate = new Date();
        String labelDate = new SimpleDateFormat("dd/MM/yyyy").format(currentDate);
        String labelTime = new SimpleDateFormat("HH:mm").format(currentDate);
        consultationRecordDate.setText(labelDate);
        consultationRecordTime.setText("Consultation Time:    " + labelTime);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        consultationReportButtonGroup = new javax.swing.ButtonGroup();
        doctorOptions = new javax.swing.JTabbedPane();
        searchReports = new javax.swing.JPanel();
        searchDiagnosisLabel = new javax.swing.JLabel();
        searchDiagnosisTextfield = new javax.swing.JTextField();
        searchDiagnosisBtn = new javax.swing.JButton();
        searchPrescripLabel = new javax.swing.JLabel();
        searchPrescriptionTextfield = new javax.swing.JTextField();
        searchPrescripBtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        searchReportHistory = new javax.swing.JTable();
        searchClearBtn = new javax.swing.JButton();
        consultationReport = new javax.swing.JPanel();
        consultationIllnessLabel = new javax.swing.JLabel();
        consultationPrescripLabel = new javax.swing.JLabel();
        consultationIllnessTextfield = new javax.swing.JTextField();
        consultationPrescripTextfield = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        consultationPatientHistory = new javax.swing.JTable();
        consultationQuantityLabel = new javax.swing.JLabel();
        consultationQuantityTextfield = new javax.swing.JTextField();
        consultationSaveButton = new javax.swing.JButton();
        consultationDateLabel = new javax.swing.JLabel();
        consultationRecordDate = new javax.swing.JLabel();
        consultationRecordTime = new javax.swing.JLabel();
        consultationPrescripPillsBtn = new javax.swing.JRadioButton();
        consultationPrescripLiquidBtn = new javax.swing.JRadioButton();
        consultationDisplayAll = new javax.swing.JButton();
        consultationClearBtn = new javax.swing.JButton();
        consultantSaveTable = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        searchDiagnosisLabel.setText("Diagnosis Name:");

        searchDiagnosisBtn.setText("Search Diagnosis");
        searchDiagnosisBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchDiagnosisBtnActionPerformed(evt);
            }
        });

        searchPrescripLabel.setText("Prescription Name:");

        searchPrescripBtn.setText("Search Prescription");
        searchPrescripBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchPrescripBtnActionPerformed(evt);
            }
        });

        searchReportHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Date", "Patient Name", "Diagnosis", "Prescription"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(searchReportHistory);

        searchClearBtn.setText("Clear table");
        searchClearBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchClearBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout searchReportsLayout = new javax.swing.GroupLayout(searchReports);
        searchReports.setLayout(searchReportsLayout);
        searchReportsLayout.setHorizontalGroup(
            searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchReportsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE)
                    .addGroup(searchReportsLayout.createSequentialGroup()
                        .addGroup(searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(searchPrescripLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(searchDiagnosisLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(searchDiagnosisTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                            .addComponent(searchPrescriptionTextfield))
                        .addGap(18, 18, 18)
                        .addGroup(searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(searchDiagnosisBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(searchPrescripBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(searchReportsLayout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(searchClearBtn)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        searchReportsLayout.setVerticalGroup(
            searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchReportsLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchDiagnosisLabel)
                    .addComponent(searchDiagnosisTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchDiagnosisBtn))
                .addGap(33, 33, 33)
                .addGroup(searchReportsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchPrescripLabel)
                    .addComponent(searchPrescriptionTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchPrescripBtn))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(searchClearBtn)
                .addContainerGap(133, Short.MAX_VALUE))
        );

        doctorOptions.addTab("Search reports", searchReports);

        consultationIllnessLabel.setText("Illness Description: ");

        consultationPrescripLabel.setText("Prescription:");

        consultationPatientHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Date", "Illness Description", "Prescription", "Quantity"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(consultationPatientHistory);

        consultationQuantityLabel.setText("Quantity:");

        consultationSaveButton.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        consultationSaveButton.setText("Save Entry");
        consultationSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultationSaveButtonActionPerformed(evt);
            }
        });

        consultationDateLabel.setText("Consultation Date:");

        consultationPrescripPillsBtn.setText("Pills");
        consultationPrescripPillsBtn.setFocusCycleRoot(true);

        consultationPrescripLiquidBtn.setText("Liquid");

        consultationDisplayAll.setText("Display in table");
        consultationDisplayAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultationDisplayAllActionPerformed(evt);
            }
        });

        consultationClearBtn.setText("clear table");
        consultationClearBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultationClearBtnActionPerformed(evt);
            }
        });

        consultantSaveTable.setText("save table");
        consultantSaveTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultantSaveTableActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout consultationReportLayout = new javax.swing.GroupLayout(consultationReport);
        consultationReport.setLayout(consultationReportLayout);
        consultationReportLayout.setHorizontalGroup(
            consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(consultationReportLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(consultationReportLayout.createSequentialGroup()
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE)
                            .addGroup(consultationReportLayout.createSequentialGroup()
                                .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(consultationQuantityLabel)
                                    .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(consultationReportLayout.createSequentialGroup()
                                            .addGap(301, 301, 301)
                                            .addComponent(consultationPrescripPillsBtn)
                                            .addGap(18, 18, 18)
                                            .addComponent(consultationPrescripLiquidBtn))
                                        .addGroup(consultationReportLayout.createSequentialGroup()
                                            .addGap(0, 486, Short.MAX_VALUE)
                                            .addComponent(consultationClearBtn))))
                                .addGap(107, 107, 107)))
                        .addContainerGap())
                    .addGroup(consultationReportLayout.createSequentialGroup()
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(consultationPrescripTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(consultationRecordTime, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, consultationReportLayout.createSequentialGroup()
                                        .addComponent(consultationDateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(consultationRecordDate, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(consultationReportLayout.createSequentialGroup()
                                    .addComponent(consultationIllnessLabel)
                                    .addGap(27, 27, 27)
                                    .addComponent(consultationIllnessTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(consultationQuantityTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(consultationReportLayout.createSequentialGroup()
                                .addGap(95, 95, 95)
                                .addComponent(consultationSaveButton)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, consultationReportLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(consultationDisplayAll)
                                .addGap(146, 146, 146))))
                    .addGroup(consultationReportLayout.createSequentialGroup()
                        .addComponent(consultationPrescripLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(consultantSaveTable)
                        .addGap(104, 104, 104))))
        );
        consultationReportLayout.setVerticalGroup(
            consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(consultationReportLayout.createSequentialGroup()
                .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(consultationReportLayout.createSequentialGroup()
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(consultationReportLayout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(consultationDateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                                    .addComponent(consultationRecordDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(consultationRecordTime, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(consultationReportLayout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(consultationSaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(consultationIllnessLabel)
                                .addComponent(consultationIllnessTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(consultationDisplayAll, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(consultationReportLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(consultationClearBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(consultationReportLayout.createSequentialGroup()
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(consultationPrescripLabel)
                            .addComponent(consultationPrescripTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(consultationReportLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(consultationQuantityLabel)
                            .addComponent(consultationQuantityTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(consultationPrescripPillsBtn)
                            .addComponent(consultationPrescripLiquidBtn))
                        .addGap(35, 35, 35))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, consultationReportLayout.createSequentialGroup()
                        .addComponent(consultantSaveTable)
                        .addGap(65, 65, 65)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        consultationQuantityLabel.getAccessibleContext().setAccessibleName("Quantity");

        doctorOptions.addTab("Consultation Report", consultationReport);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(doctorOptions)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(doctorOptions)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void consultationSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultationSaveButtonActionPerformed
        extractFileName();
        getAllRecord();
        saveConsultationEntry();
    }//GEN-LAST:event_consultationSaveButtonActionPerformed

    private void consultationDisplayAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultationDisplayAllActionPerformed
        consultationDisplayAllRecords();
    }//GEN-LAST:event_consultationDisplayAllActionPerformed

    private void searchDiagnosisBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchDiagnosisBtnActionPerformed
        searchDiagnosis();
    }//GEN-LAST:event_searchDiagnosisBtnActionPerformed

    private void searchPrescripBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchPrescripBtnActionPerformed
        searchPrescript();
    }//GEN-LAST:event_searchPrescripBtnActionPerformed

    private void consultationClearBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultationClearBtnActionPerformed
        clearConsultationTable();
    }//GEN-LAST:event_consultationClearBtnActionPerformed

    private void searchClearBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchClearBtnActionPerformed
        clearSearchTable();
    }//GEN-LAST:event_searchClearBtnActionPerformed

    private void consultantSaveTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultantSaveTableActionPerformed
        new ReplaceFileContents().replace();
        //new FileDelete().deleteFile();
        
        int row = consultationPatientHistory.getSelectedRow();
//        StringBuilder sb = new StringBuilder();
//        sb.append("");
//        sb.append(row);
//        String selectedrow = sb.toString();
//        
//        System.out.println(selectedrow);
//        
//        int column = consultationPatientHistory.getColumnCount();   
//        try {
//                File f = new File("C:\\Users\\382499\\Documents\\111.txt");
//                Scanner scan = new Scanner(new FileInputStream(f));
//                String[] array;
//                while (scan.hasNextLine()) {
//                    String line = scan.nextLine();
//                    if (line.indexOf(",") > -1) {
//                        array = line.split(",");
//                    } else {
//                        array = line.split("\n");
//                    }
//                    Object[] data = new Object[array.length];
//                    for (int i = 0; i < array.length; i++) {
//                        data[i] = array[i];
//                    }
//                    if (line.matches(row)) {
//                        searchReportdtm.addRow(data);
//                    }
//                }
//            } catch (Exception e) {
//                noSuchFile();
//                searchDiagnosisTextfield.requestFocus();
//            }
        
//        for (int i = 0; i < column; i++) {
//            System.out.println(consultationPatientHistory.getValueAt(row, i));
//        }
//        
//        String oldFileName = "C:\\Users\\382499\\Documents\\111.txt";
//      String tmpFileName = "C:\\Users\\382499\\Documents\\111BK.txt";
//
//      BufferedReader br = null;
//      BufferedWriter bw = null;
//      try {
//         br = new BufferedReader(new FileReader(oldFileName));
//         bw = new BufferedWriter(new FileWriter(tmpFileName));
//         String line;
//         while ((line = br.readLine()) != null) {
//            if (line.contains(consultationIllnessTextfield.getText()))
//               line = line.replace(consultationIllnessTextfield.getText(), consultationIllnessTextfield.getText());
//            bw.write(line+ "\r\n");
//         }
//      } catch (Exception e) {
//         return;
//      } finally {
//         try {
//            if(br != null)
//               br.close();
//         } catch (IOException e) {
//            //
//         }
//         try {
//            if(bw != null)
//               bw.close();
//         } catch (IOException e) {
//            //
//         }
//      }
//      // Once everything is complete, delete old file..
//      File oldFile = new File(oldFileName);
//      oldFile.delete();
//
//      // And rename tmp file's name to old file name
//      File newFile = new File(tmpFileName);
//      newFile.renameTo(oldFile);
//                        
    }//GEN-LAST:event_consultantSaveTableActionPerformed

    private void consultantRecordWriteAll() {
        BufferedWriter buf;
        try {
            buf = new BufferedWriter(new FileWriter(file, true));
            if (CRPrescription == "Pill") {
                buf.write(CRD + "," + CRPN + "," + CRDIDT + "," + CRDPT + "," + CRQT + " pills," + CRPrescription);
                buf.newLine();
                buf.close();
                JOptionPane.showMessageDialog(this, "Data Saved");
            } else {
                buf.write(CRD + "," + CRPN + "," + CRDIDT + "," + CRDPT + "," + CRQT + " ml," + CRPrescription);
                buf.newLine();
                buf.close();
                JOptionPane.showMessageDialog(this, "Data Saved");
            }
        } catch (Exception e) {
        }
    }

    private void searchPrescript() {
        if (searchPrescriptionTextfield.getText().isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Please key in Prescription to search. ");
            searchPrescriptionTextfield.requestFocus();
        } else {

            try {
                File f = new File("C:\\Users\\382499\\Documents\\111.txt");
                Scanner scan = new Scanner(new FileInputStream(f));
                String[] array;
                while (scan.hasNextLine()) {
                    String line = scan.nextLine();
                    if (line.indexOf(",") > -1) {
                        array = line.split(",");
                    } else {
                        array = line.split("\n");
                    }
                    Object[] data = new Object[array.length];
                    for (int i = 0; i < array.length; i++) {
                        data[i] = array[i];
                    }
                    if (line.contains(searchPrescriptionTextfield.getText())) {
                        searchReportdtm.addRow(data);
                    }
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "No such file.");
                consultationDisplayAll.requestFocus();
            }
        }
    }

    private void searchDiagnosis() {

        if (searchDiagnosisTextfield.getText().isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Please key in Diagnosis to search. ");
            searchDiagnosisTextfield.requestFocus();
        } else {
            try {
                File f = new File("C:\\Users\\382499\\Documents\\111.txt");
                Scanner scan = new Scanner(new FileInputStream(f));
                String[] array;
                while (scan.hasNextLine()) {
                    String line = scan.nextLine();
                    if (line.indexOf(",") > -1) {
                        array = line.split(",");
                    } else {
                        array = line.split("\n");
                    }
                    Object[] data = new Object[array.length];
                    for (int i = 0; i < array.length; i++) {
                        data[i] = array[i];
                    }
                    if (line.contains(searchDiagnosisTextfield.getText())) {
                        searchReportdtm.addRow(data);
                    }
                }
            } catch (Exception e) {
                noSuchFile();
                searchDiagnosisTextfield.requestFocus();
            }
        }
    }

    private void consultationDisplayAllRecords() {
        try {
            openFileChooser();
            File f = new File("C:\\Users\\382499\\Documents\\111.txt");
            Scanner scan = new Scanner(new FileInputStream(f));
            String[] array;
            while (scan.hasNextLine()) {
                String line = scan.nextLine();
                if (line.indexOf(",") > -1) {
                    array = line.split(",");
                } else {
                    array = line.split("\n");
                }
                Object[] data = new Object[array.length];
                for (int i = 0; i < array.length; i++) {
                    data[i] = array[i];
                }
                consultantReportdtm.addRow(data);
            }
        } catch (Exception e) {
            noSuchFile();
            consultationDisplayAll.requestFocus();
        }
    }
    
    private void saveConsultationEntry() {
        if (consultationIllnessTextfield.getText().isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Please enter illness description!");
            consultationIllnessTextfield.requestFocus();
        } else {
            if (consultationPrescripTextfield.getText().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "Please enter prescription!");
                consultationPrescripTextfield.requestFocus();
            } else {
                if (consultationQuantityTextfield.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(rootPane, "Please enter prescription quantity!");
                    consultationQuantityTextfield.requestFocus();
                } else {
                    if (buttonGroup.getSelection() == null) {
                        JOptionPane.showMessageDialog(rootPane, "Please select prescription type!");
                        consultationQuantityTextfield.requestFocus();
                    } else {
                        openFileChooser();
                        if (CRPrescription == "Pill") {
                            consultantReportdtm.addRow(new Object[]{CRD, CRPN, CRDIDT, CRDPT, CRQT + " pills", CRPrescription});
                        } else {
                            consultantReportdtm.addRow(new Object[]{CRD, CRPN, CRDIDT, CRDPT, CRQT + " ml", CRPrescription});
                        }

                        file = fc.getSelectedFile();
                        consultantRecordWriteAll();

                        consultationIllnessTextfield.setText("");
                        consultationPrescripTextfield.setText("");
                        consultationQuantityTextfield.setText("");
                        buttonGroup.clearSelection();
                    }
                }
            }
        }        
    }

    public void getAllRecord() {
        CRD = consultationRecordDate.getText();
        CRDIDT = consultationIllnessTextfield.getText();
        CRDPT = consultationPrescripTextfield.getText();
        CRQT = consultationQuantityTextfield.getText();
        if (consultationPrescripPillsBtn.isSelected()) {
            CRPrescription = "Pill";
        } else {
            CRPrescription = "Liquid";
        }
    }

    public void extractFileName() {

        File f = new File("C:\\Users\\382499\\Documents\\111.txt");
        String name = f.getName();
        int pos = name.lastIndexOf(".");
        if (pos > 0) {
            name = name.substring(0, pos);
        }

        CRPN = name;
    }

    public void openFileChooser() {
        fc = new JFileChooser();                    //Choosing the path 
        int option = fc.showSaveDialog(this);       //Opening the dialog box
    }

    public void noSuchFile() {
        JOptionPane.showMessageDialog(rootPane, "No such file.");
    }

    public void clearConsultationTable() {
        if (consultationPatientHistory.getRowCount() > 0) {
            for (int i = consultationPatientHistory.getRowCount() - 1; i > -1; i--) {
                consultantReportdtm.removeRow(i);
            }
        }
    }

    public void clearSearchTable() {
        if (searchReportHistory.getRowCount() > 0) {
            for (int i = searchReportHistory.getRowCount() - 1; i > -1; i--) {
                searchReportdtm.removeRow(i);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Doctorpanel.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Doctorpanel.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Doctorpanel.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Doctorpanel.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Doctorpanel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton consultantSaveTable;
    private javax.swing.JButton consultationClearBtn;
    private javax.swing.JLabel consultationDateLabel;
    private javax.swing.JButton consultationDisplayAll;
    private javax.swing.JLabel consultationIllnessLabel;
    private javax.swing.JTextField consultationIllnessTextfield;
    private javax.swing.JTable consultationPatientHistory;
    private javax.swing.JLabel consultationPrescripLabel;
    private javax.swing.JRadioButton consultationPrescripLiquidBtn;
    private javax.swing.JRadioButton consultationPrescripPillsBtn;
    private javax.swing.JTextField consultationPrescripTextfield;
    private javax.swing.JLabel consultationQuantityLabel;
    private javax.swing.JTextField consultationQuantityTextfield;
    private javax.swing.JLabel consultationRecordDate;
    private javax.swing.JLabel consultationRecordTime;
    private javax.swing.JPanel consultationReport;
    private javax.swing.ButtonGroup consultationReportButtonGroup;
    private javax.swing.JButton consultationSaveButton;
    private javax.swing.JTabbedPane doctorOptions;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton searchClearBtn;
    private javax.swing.JButton searchDiagnosisBtn;
    private javax.swing.JLabel searchDiagnosisLabel;
    private javax.swing.JTextField searchDiagnosisTextfield;
    private javax.swing.JButton searchPrescripBtn;
    private javax.swing.JLabel searchPrescripLabel;
    private javax.swing.JTextField searchPrescriptionTextfield;
    private javax.swing.JTable searchReportHistory;
    private javax.swing.JPanel searchReports;
    // End of variables declaration//GEN-END:variables
}